﻿package lib 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author kabuto.boy
	 */
	public class Scrubbar extends Sprite
	{
		private var _scrubber:Scrubber;
		private var _filledbar:Sprite;
		
		private var eScrubed:Event = new Event("scrubed");
		
		public var position:Number;
		
		public function Scrubbar() 
		{
			this.alpha = 0;
			
			_scrubber = (this.parent as Object).scrubber;
			_filledbar = (this.parent as Object).filledbar;
			
			this.addEventListener(MouseEvent.MOUSE_DOWN, onEvent);
		}
		
		private function onEvent(event:*):void
		{
			switch(event.type) {
				case "mouseDown" :
					var _x:Number = this.mouseX + this.x;
					try {
						_scrubTo(_x);
					} catch (error:*) {
						_scrubber = (this.parent as Object).scrubber;
						_filledbar = (this.parent as Object).filledbar;
						_scrubTo(_x);
					}
					
					position = this.mouseX / this.width;
					
					this.dispatchEvent(eScrubed);
					break;
			}
		}
		
		private function _scrubTo(value:Number):void
		{
			_scrubber.x = value;
			_filledbar.width = value - this.x;
		}
		
	}

}